﻿var _app = navigator.appName;

function powerConfig(args) {
	var defaults = { "width":170, "height":35, "maxLength":20, "minLength":6, "maskChar":"#", "backColor":"#FFFFFF", "textColor":"#555555", "borderColor":"#ffffff", "accepts":"\\w{0,}", "caption":"泰海网络科技服务有限公司", "captionColor":"#FFFFFF", "captionFont":"", "captionSize":0, "captionBold":"true", "lang":"zh_CN"};
	for (var p in args)
		if (args[p] != null) defaults[p] = args[p];
	return defaults;
}

function writePluginObject(oid, clsid, cfg) {
	var tempHtml = '';
	tempHtml = '<object id="' + oid + '" type="' + clsid
		+ '" width="' + cfg.width + '" height="' + cfg.height
		+ '" style="width:' + cfg.width + 'px;height:' + cfg.height + 'px;padding-top:8px;" class="lg_input">';
	for (var name in cfg){
		tempHtml += '<param name="' + name + '" value="' + cfg[name] + '">';	
	}
	tempHtml += '</object>';
	return tempHtml;
};

function writeObject(oid, clsid, cfg) {
	var tempHtml = '';
	tempHtml='<div style="display:inline;" class="lg_obj">';
	tempHtml+= '<object id="' + oid + '" codebase="../ocx/PowerEnterSF.CAB#version=2,3,3,30" classid="' + clsid
			+ '" width="' + cfg.width + '" height="' + cfg.height
			+ '" style="width:' + cfg.width + 'px;height:' + cfg.height + 'px;height:20px;" class="">';
	
	for (var name in cfg){
		tempHtml += '<param name="' + name + '" value="' + cfg[name] + '">';
	}
	tempHtml += '</object>';
	tempHtml+='</div>';
	return tempHtml;
};

function writeEditObject(oid, cfg) {
	if (!oid || typeof(oid) != "string") {
		alert("writePassObj Failed: oid are required!");
	} else {
		if (_app == 'Microsoft Internet Explorer')
		{
			var tempHtml = writeObject(oid+"_ie", "clsid:0B972388-943A-4E84-9B48-FB98C9861BCE", powerConfig(cfg));
			//setPEXSetupUrl("setssww");
			return tempHtml;
		}
		else
		{
			var tempHtml = writePluginObject(oid+"_noie", "application/x-vnd-csii-powerenter-sf", powerConfig(cfg));
			//setPEXSetupUrl(oid+"_noie");
			return tempHtml;
		}
	}
};

function writePassObject(oid, cfg) {
	if (!oid || typeof(oid) != "string") {
		alert("writePassObj Failed: oid are required!");
	} else {
		if (_app == 'Microsoft Internet Explorer')
		{
			var tempHtml = writeObject(oid+"_ie", "clsid:BCB9478C-D598-45CD-9983-7623EC1ED953", powerConfig(cfg));
			//setPEXSetupUrl("setssww");
			return tempHtml;
		}
		else
		{
			var tempHtml = writePluginObject(oid+"_noie", "application/x-vnd-csii-powerenter-sf", powerConfig(cfg));
			//setPEXSetupUrl(oid+"_noie");
			return tempHtml;
		}
	}
};

function writeUtilObject(oid, cfg) {
	if (!oid || typeof(oid) != "string") {
		alert("writePassObj Failed: oid are required!");
	} else {
		if (_app == 'Microsoft Internet Explorer')
		{
			return writeObject(oid+"_ie", "clsid:6E181EEA-F0F5-4983-A2EE-52D2D943A575", powerConfig(cfg));
		}
		else
		{
			return writePluginObject(oid+"_noie", "application/x-vnd-csii-powerenter-sf", powerConfig(cfg));
		}
		
	}
};

function getIBSInput(id, ts, spanId,massage){
    try{    	
		var powerobj = document.getElementById(getObjID(id));	
		powerobj.setTimestamp(ts);
		var nresult = powerobj.verify();
		if(nresult < 0)
		{			
			var error;
			if(nresult == -1)
			{
				error = "内容不能为空";
			}
			else if(nresult == -2)
			{
				error = "输入长度不足";
			}
			else if(nresult == -3)
			{
				error = "输入内容不合法";
			}
			else
			{
				error = powerobj.lastError(); 
			}
			//PEGetElement(spanId).innerHTML = massage +error;
			$(".tip").show();
			$(".tip").empty();
			$(".tip").html(massage +error);
			return null;
		}	
				
		value = powerobj.getValue();
		if(value=="")
		{
			//PEGetElement(spanId).innerHTML= massage+powerobj.lastError();
			$(".tip").show();
			$(".tip").empty();
			$(".tip").html(massage+powerobj.lastError());
			return null;
		}
		else
		{
			return value;
		}
	}
	catch(e)
	{
		//PEGetElement(spanId).innerHTML= massage +e.message;
		$(".tip").show();
		$(".tip").empty();
		$(".tip").html(massage);
	}
	return null;
}

function getMFMInput(id, ts, spanId,massage) 
{
    try 
    {
		var powerobj = document.getElementById(getObjID(id));	
		powerobj.setTimestamp(ts);
		value = powerobj.getMFM();
		if(value=="")
		{
			PEGetElement(spanId).innerHTML= massage + powerobj.lastError(); 
			return null;
		}
		else
		{
			return value;
		}
	}
	catch(e)
	{
		PEGetElement(spanId).innerHTML= massage + e.message; 
	}
	return null;
}

function PEGetElement(id)
{
	return  window.document.getElementById(id);
}

function getObjID(id)
{
	if (_app == 'Microsoft Internet Explorer'){
		id=id+"_ie";
	}else{
		id=id+"_noie";
	}
	return id;
}

function isActiveXRegistered(ctlname){
	try{
   		var comActiveX = new ActiveXObject(ctlname);   
	}catch(e){
		return false;   
	}
	return true;
}

function setPEXSetupUrl(oid,msid)
{
	if (_app != 'Microsoft Internet Explorer')
	{
		var powerEnterPlugin = navigator.plugins["PowerEnter Plug-in for SF"];
        if(!powerEnterPlugin){
        	PEGetElement(msid).innerHTML = '<a href="../security/download" class="download_install">点击此处下载控件</a>';
        }
	}else{
		if(isActiveXRegistered("POWERENTERSF.PowerEditXSFCtrl.1")==false){
			PEGetElement(msid).innerHTML = '<a href="../security/download" class="download_install">点击此处下载控件</a>';
		}else{
			try{
				var tmp = document.getElementById(oid);
				if(tmp.getVersion()<2003003030){
					PEGetElement(msid).innerHTML = '<a href="../security/download" class="download_install">点击此处更新控件</a>';
				}
			}catch(e){
				PEGetElement(msid).innerHTML = '<a href="../security/download" class="download_install">点击此处下载控件</a>';
			}
		}
	}
}


